package web.PetStore.persistence.impl;

import com.mysql.cj.x.protobuf.MysqlxSession;
import web.PetStore.domain.Account;
import web.PetStore.persistence.AccountDAO;
import web.PetStore.persistence.DBUtil;
import web.PetStore.service.AccountService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountDAOImpl implements AccountDAO {
    private static final String FIND_USER_BY_ID_AND_PASSWORD =
            "SELECT * FROM account WHERE user_id=? AND password=?";
    private static final String FIND_USER_BY_ID =
            "SELECT * FROM account WHERE user_id=?";
    private static final String FIND_USER_BY_ID_AND_PHONE =
            "SELECT * FROM account WHERE user_id=? AND phone=?";
    private static final String ADD_USER =
            "INSERT INTO account (user_id,password,phone,email,address,status) VALUES (?,?,?,?,?,'User')";
    private static final String CHANGE_INFO =
            "UPDATE account SET phone=?,email=?,address=? WHERE user_id=?";
    private static final String CHANGE_PASSWORD =
            "UPDATE account SET password=? WHERE user_id=?";

    @Override
    public Account findUserByIdAndPassword(String userid, String password) {
        Account account = null;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(FIND_USER_BY_ID_AND_PASSWORD);
            preparedStatement.setString(1,userid);
            preparedStatement.setString(2,password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                account = toAccount(resultSet);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public boolean findUserById(String userid) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(FIND_USER_BY_ID);
            preparedStatement.setString(1,userid);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                result = true;
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Account findUserByIdAndPhone(String userid, String phone) {
        Account user = null;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(FIND_USER_BY_ID_AND_PHONE);
            preparedStatement.setString(1,userid);
            preparedStatement.setString(2,phone);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                user = toAccount(resultSet);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean addUser(Account account) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_USER);
            preparedStatement.setString(1,account.getUserid());
            preparedStatement.setString(2,account.getPassword());
            preparedStatement.setString(3,account.getPhone());
            preparedStatement.setString(4,account.getEmail());
            preparedStatement.setString(5,account.getAddress());
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                result = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean changeInfo(Account account) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(CHANGE_INFO);
            preparedStatement.setString(1,account.getPhone());
            preparedStatement.setString(2,account.getEmail());
            preparedStatement.setString(3,account.getAddress());
            preparedStatement.setString(4,account.getUserid());
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                result = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean changePassword(String password,String userid) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(CHANGE_PASSWORD);
            preparedStatement.setString(1,password);
            preparedStatement.setString(2,userid);
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                result = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    private Account toAccount(ResultSet resultSet){
        Account account = new Account();
        try {
            account.setUserid(resultSet.getString("user_id"));
            account.setAddress(resultSet.getString("address"));
            account.setPassword(resultSet.getString("password"));
            account.setEmail(resultSet.getString("email"));
            account.setPhone(resultSet.getString("phone"));
            account.setStatus(resultSet.getString("status"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return account;
    }
}
